# application/models/withstand.py
from application.models.base import Base
from sqlalchemy import Column, String, Date, Text, Integer
from datetime import datetime


class Withstand(Base):
    """耐压试验记录模型"""

    __tablename__ = "withstand"

    # 主键
    withstand_id = Column(String(32), primary_key=True, comment="耐压试验ID")

    # 试验编号
    withstand_no = Column(String(50), nullable=False, comment="试验编号")

    # 产品编号
    product_no = Column(String(50), nullable=False, comment="产品编号")

    # 试验人员
    withstand_person = Column(String(50), nullable=False, comment="试验人员")

    # 监督人员
    supervise_person = Column(String(50), nullable=False, comment="监督人员")

    # 试验日期
    withstand_date = Column(Date, nullable=False, comment="试验日期")

    # 备注
    remarks = Column(Text, comment="备注")

    # 配方ID
    recipe_id = Column(String(32), comment="配方ID")

    # 耐压值
    withstand_value = Column(Text, comment="耐压值")

    # 局放值
    partial_discharge_value = Column(Text, comment="局放值")

    # 放电量最大值
    discharge_max_value = Column(String(32), comment="放电量最大值")

    # 放电量最小值
    discharge_min_value = Column(String(32), comment="放电量最小值")

    # 耐压时间
    withstand_time = Column(Text, comment="耐压时间")

    # 图片路径
    image_path = Column(String(255), comment="图片路径")

    # 耐压结果
    withstand_result = Column(Text, comment="耐压结果")

    # 放电结果
    discharge_result = Column(Text, comment="放电结果")

    # 图表数据文件
    chart_data_file = Column(String(255), comment="图表数据文件")

    def __repr__(self):
        return f"<Withstand {self.withstand_id}: {self.withstand_no}>"

    def to_dict(self):
        """转换为字典格式"""
        return {
            "withstand_id": self.withstand_id,
            "withstand_no": self.withstand_no,
            "product_no": self.product_no,
            "withstand_person": self.withstand_person,
            "supervise_person": self.supervise_person,
            "withstand_date": (
                self.withstand_date.isoformat() if self.withstand_date else None
            ),
            "remarks": self.remarks,
            "recipe_id": self.recipe_id,
            "withstand_value": self.withstand_value,
            "partial_discharge_value": self.partial_discharge_value,
            "discharge_max_value": self.discharge_max_value,
            "discharge_min_value": self.discharge_min_value,
            "withstand_time": self.withstand_time,
            "image_path": self.image_path,
            "withstand_result": self.withstand_result,
            "discharge_result": self.discharge_result,
            "chart_data_file": self.chart_data_file,
            "is_del": self.is_del,
            "create_dept": self.create_dept,
            "create_by": self.create_by,
            "create_time": self.create_time.isoformat() if self.create_time else None,
            "update_by": self.update_by,
            "update_time": self.update_time.isoformat() if self.update_time else None,
        }
